<template>
   <div class="app-container">
        <el-row :gutter="20">
            <el-col :span="24" :xs="24">
                <!-- 搜索框 -->
                <el-form   ref="queryRef" :inline="true"  v-show="showSearch">
                    <el-form-item label="纵向课题等级" prop="leader" label-width="115px"> 
                        <el-select
                        placeholder="请选择课题等级"
                        clearable
                        size="small"
                        style="width:  260px"
                        v-model="queryParams.topicLevel"
                    >
                    <el-option
                        v-for="item in topicLevel"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                    </el-select>
                    </el-form-item>
                    <el-form-item label="纵向课题类型" prop="category" label-width="115px">
                        <el-input
                            v-model="queryParams.topicType"
                            placeholder="请输入纵向课题类型"
                            clearable
                            size="small"
                            style="width: 240px"
                        />
                    </el-form-item>
                    <el-form-item>
                    <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
                    <el-button icon="Tickets" size="mini" @click="handleRef">参考</el-button>
                    </el-form-item>
                </el-form>
                <!-- 操作按钮 -->
                <el-row :gutter="10"  class="mb8" >
                    <el-col :span="1.5">
                        <el-button
                            type="primary"
                            plain
                            icon="Plus"
                            size="mini"
                            @click="handleAdd"

                        >新增</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                            type="success"
                            plain
                            icon="Edit"
                            size="mini"
                            :disabled="single"
                            @click="handleUpdate"

                        >修改</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button
                            type="danger"
                            plain
                            icon="Check"
                            size="mini"
                            :disabled="single"
                            @click="handleDelete"
                        >删除</el-button>
                    </el-col>
                    <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
                </el-row>
                <!-- 展示列表 -->
                <el-table  :data="topicList" v-loading="loading" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="50" align="center" />
                  <el-table-column label="代码"  align="center" prop="code" width="80"  />
                    <el-table-column label="课题类型" align="center" prop="topicType" overflow:hidden  :show-overflow-tooltip="true"/>
                    <el-table-column label="课题等级" align="center" prop="topicLevel"  width="90" />
                    <el-table-column label="工作量Max" align="center" prop="workloadMax"  width="90" />
                    <el-table-column label="工作量Min" align="center" prop="workloadMin"  width="90" />
                    <el-table-column label="备注" align="center" prop="remark" width="100" overflow:hidden  :show-overflow-tooltip="true"/>                    
                    <el-table-column label="更新时间" align="center" prop="updatedTime" width="160" />
                    <el-table-column label="创建时间" align="center" prop="createdTime" width="160"/>
                    <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
                        <template #default="scope" >
                            <el-button
                                size="mini"
                                type="text"
                                icon="Edit"
                                @click="handleUpdate(scope.row)"
                            >修改</el-button>
                            <el-button
                                size="mini"
                                type="text"
                                icon="Delete"
                                @click="handleDelete(scope.row)"
                            >删除</el-button>
                        </template>
                    </el-table-column>   
                </el-table>
                    <pagination
                        v-show="total > 0"
                        :total="total"
                        v-model:page="queryParams.pageNum"
                        v-model:limit="queryParams.pageSize"
                        @pagination="getList"
                    />
            </el-col>
        </el-row>
        <!-- 新增或修改弹窗 -->
        <el-dialog :title="title" v-model="open" width="400px" append-to-body>
            <el-form ref="issuesDictRef" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="分类代码" prop="code">
               <el-input  placeholder="请输入分类代码" v-model="form.code"/>
          </el-form-item>
                <el-form-item label="课题等级" prop="topicType">
                    <el-select
                        placeholder="请选择课题等级"
                        clearable
                        style="width:  260px"
                        v-model="form.topicLevel"
                    >
                    <el-option
                        @click="topicLevelSelect"
                        v-for="item in topicLevel"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                    </el-select>
                </el-form-item>
                <el-form-item label="课题类型" prop="topicType">
                    <el-input 
                    placeholder="请输入课题类型"
                    v-model="form.topicType"
                    />

                </el-form-item>
                          <el-form-item label="工作量Max" prop="workloadMax">
               <el-input-number  :min="0"
                                 placeholder="请输入工作量最大值" 
                                 controls-position="right"
                                 v-model="form.workloadMax"
                                 style="width: 260px"/>
          </el-form-item>
          <el-form-item label="工作量Min" prop="workloadMin">
               <el-input-number  :min="0"
                                 placeholder="请输入工作量最小值" 
                                 controls-position="right"
                                 v-model="form.workloadMin"
                                 style="width: 260px"/>
          </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary"  @click="submitForm">确 定</el-button>
                    <el-button @click="cancel" >取 消</el-button>
                </div>
            </template>
    </el-dialog>
     <!-- 参考表单对话框 -->
<el-dialog :title="title" v-model="refOpen" width="850px" append-to-body>
    <el-table :data="refData" border style="width: 100%">
            <el-table-column prop="number" label ="序 号" width="80" align="center"/>
            <el-table-column prop="paper" label ="论文、成果转化"  align="center" />
            <el-table-column prop="category" label="类别" width="100" align="center" />
            <el-table-column prop="workload" label="工作量" width="100" align="center" />
        </el-table>
</el-dialog>
    </div>
</template>

<script setup>
import { listAll , addDict , delDict , getDict , updateDict} from "@/api/nepu_dict/topic"
import { reactive } from "vue"

const refData = [
   {
      number:'1',
      paper:'国家科技重大专项、国家重点研发计划、国家自然科学基金重大项目、国家社会科学基金重大项目',
      category:'A1',
      workload:'2000*Y'
   },
    {
      number:'2',
      paper:'国家自然科学基金重点项目、国家社会科学基金重点项目',
      category:'A2',
      workload:'600*Y'
   }
   ,
    {
      number:'3',
      paper:'国家自然科学基金（含青年）、国家社会科学基金（含青年、教育学项目）、国家艺术基金',
      category:'A3',
      workload:'500*Y'
   },
   {
      number:'4',
      paper:'全国教育科学规划国家级课题、国际科技合作专项、国家重大科学仪器设备开发专项、国家磁约束核聚变能发展研究专项、国家科技基础性工作专项、国家软科学研究计划、武器装备重点型号项目、国家级教改工程项目、国家级新工科和新文科研究与实践项目等教学类项目',
      category:'A4',
      workload:'400*Y'
   },
   {
      number:'5',
      paper:'中央和国家机关委托项目、教育部人文社科项目、全国古委会项目',
      category:'A5',
      workload:'350*Y'
   },
   {
      number:'6',
      paper:'省级重大项目（基金类）',
      category:'B1',
      workload:'250*Y'
   },
   {
      number:'7',
      paper:'省级重点项目（基金类）[含认定为省部级的国家科技重大专项子课题]',
      category:'B2',
      workload:'220*Y'
   },
    {
      number:'8',
      paper:'省级基金项目、部级一般项目、教育部产学合作协同育人项目、省级新工科和新文科研究与实践项目、省级高等教育教学改革研究项目等教学类项目',
      category:'B3',
      workload:'100*Y'
   },
   {
      number:'9',
      paper:'其他省级项目',
      category:'B4',
      workload:'60*Y'
   },
   {
      number:'10',
      paper:'厅局级重点项目',
      category:'C1',
      workload:'30*Y'
   },
   {
      number:'11',
      paper:'厅局级一般项目、校级高等教育教学改革研究项目、校级新工科和新文科研究与实践项目等教学类项目',
      category:'C2',
      workload:'20*Y'
   },
]
   const showSearch = ref(true);
    const ids = ref([]) 
    const single = ref(true);
    const multiple = ref(true);
    const title = ref("")
    const open  = ref(false)
    const refOpen = ref(false)
    const total = ref(0)
    const topicList = ref([])
    const loading = ref(false)
    const topicLevel = ref([
        {value: 'A1',label: 'A1'},
        {value: 'A2',label: 'A2'},
        {value: 'A3',label: 'A3'},
        {value: 'A4',label: 'A4'},
        {value: 'A5',label: 'A5'},
        {value: 'B1',label: 'B1'},
        {value: 'B2',label: 'B2'},
        {value: 'B3',label: 'B3'},
        {value: 'B4',label: 'B4'},
        {value: 'C1',label: 'C1'},
        {value: 'C2',label: 'C2'},
        ])
    const topicType = ref([
        {value: '1',label: '国家科技重大专项'},
        {value: '2',label: '国家重点研发计划'},
        {value: '3',label: '国家自然科学基金重大项目'},
        {value: '4',label: '国家社会科学基金重大项目'},
        {value: '5',label: '国家自然科学基金重点项目'},
        {value: '6',label: '国家社会科学基金重点项目'},
    ])
    const data = reactive({
        form: {},
        queryParams: {
            pageNum: 1,
            pageSize: 10,
            order:'DESC',
            sortBy:'id',
            topicType:undefined,
            topicLevel: undefined,
        },
         rules: {
        workloadMax: [
        { required: true, message: "不能为空", trigger: "blur" },
        { type: "number", min: 0,  message: "工作量不得小于0" },
        ],
        workloadMin: [
        { required: true, message: "不能为空", trigger: "blur" },
        { type: "number", min: 0,  message: "工作量不得小于0" },
        ],
        topicType: [
        { required: true, message: "不能为空", trigger: "blur" },
        {
            min: 2,
            max: 50,
            message: "类型名称长度必须介于 2 和 50 之间",
            trigger: "change",
        },
        ],
        topicLevel:[
        {required: true, message: "不能为空", trigger: "blur"}
        ],
        code:[
        {required: true, message: "不能为空", trigger: "blur" }
        ]
    },
    });
    const { queryParams, form , rules } = toRefs(data);
    const { proxy } = getCurrentInstance();

    function getList () {   
        loading.value = true;
        listAll(proxy.addDateRange(queryParams.value)).then(response => {
        topicList.value = response.content;
        total.value = response.totalElements;
        loading.value = false;
        })
    }

    function resetQueryParams(){
        queryParams.value = {
            pageNum: 1,
            pageSize: 10,
            order:'DESC',
            sortBy:'id',
            topicType:undefined,
            topicLevel: undefined,
        }
    }

    function submitForm(){
    proxy.$refs["issuesDictRef"].validate(valid => {
    if (valid) {
        if(form.value.id != undefined){
            updateDict(form.value).then(response => {
                        if(response.code === 200){
                            proxy.$modal.msgSuccess("修改成功");
                            open.value = false;
                            getList();
                        }else{
                            proxy.$modal.msgError(response.msg);
                        }
                    
                    });
        }else{
            addDict(form.value).then(response => {
            console.log(response)
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
            }); 
        }
            
       
    }
  });
    }

    /** 多选框选中数据 */
    function handleSelectionChange(selection) {
        ids.value = selection.map(item => item.id);
        single.value = selection.length != 1;
        multiple.value = !selection.length;
    }

    function handleQuery(){
        getList();
    }

    function handleUpdate(row){
        reset()
        const issuesIds = row.id || ids.value;
        form.value.id = issuesIds;
        getDict(issuesIds).then(response => {
            form.value = response;
            open.value = true;
            title.value = "修改";
        });
    }
    function handleDelete(row) {
        const issuesIds = row.id || ids.value;
        proxy.$modal.confirm('是否确认删除课题ID为"' + issuesIds + '"的数据项?').then(function () {
            return delDict(issuesIds);
        }).then(() => {
            getList();
            proxy.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    }
    function topicLevelSelect(){
        if(form.value.topicLevel === 'A1'){topicType.value = [
            {value: '1',label: '国家科技重大专项'},
            {value: '2',label: '国家重点研发计划'},
            {value: '3',label: '国家自然科学基金重大项目'},
            {value: '4',label: '国家社会科学基金重大项目'}
            ]
        
        
        
        }
        if(form.value.topicLevel === 'A2'){topicType.value = [
            {value: '1',label: '国家自然科学基金重点项目'},
            {value: '2',label: '国家社会科学基金重点项目'},
            ]
        
        
        
        }
    }
    function handleAdd(){
        reset()
        open.value = true
        title.value = "新增课题成果"
    }
    function cancel(){
        open.value = false
    }

    function resetQuery(){
        resetQueryParams()
        getList()
    }
    //参考
    function handleRef(){
        title.value = '参考'
        refOpen.value = true
    }
    function reset(){
        form.value = {
            topicLevel: undefined,
            topicType: undefined,
            workloadMax: 0,
            workloadMin:0,
            code:undefined
        }

    }
    getList();
</script>

<style>

</style>